﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Net.Sockets;
using System.Net;
using System.Threading;


namespace GDSJGServer
{
    class Program
    {
        
        private static int myProt = 8885;   //端口  
        static Socket serverSocket;         
        
        static void Main(string[] args)
        {
            //服务器IP地址  
            IPAddress ip = IPAddress.Any;
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Bind(new IPEndPoint(ip, myProt));  //绑定IP地址：端口  
            serverSocket.Listen(10);    //设定最多10个排队连接请求  
            Console.WriteLine("启动监听{0}成功", serverSocket.LocalEndPoint.ToString());
            //通过Clientsoket发送数据  
            Thread myThread = new Thread(ListenClientConnect);
            myThread.Start();
            Console.ReadLine();
        }

        /// <summary>  
        /// 监听客户端连接  
        /// </summary>  
        private static void ListenClientConnect()
        {
            string output = "";
            while (true)
            {
                Socket clientSocket = serverSocket.Accept();
                output = "Server Connected";
                clientSocket.Send(Encoding.GetEncoding("GB2312").GetBytes(output));

                Thread receiveThread = new Thread(ReceiveMessage);
                receiveThread.Start(clientSocket);
            }
        }

        /// <summary>  
        /// 接收消息  
        /// </summary>  
        /// <param name="clientSocket"></param>  
        private static void ReceiveMessage(object clientSocket)
        {
            Socket myClientSocket = (Socket)clientSocket;
            while (true)
            {
                try
                {
                    byte[] result = new byte[1024];
                    //通过clientSocket接收数据  
                    int receiveNumber = myClientSocket.Receive(result);
                    string data = Encoding.GetEncoding("GB2312").GetString(result, 0, receiveNumber);

                    Console.WriteLine("接收客户端{0}消息:{1}", myClientSocket.RemoteEndPoint.ToString(), data);
                    if (data.Equals(""))
                    {
                        myClientSocket.Close();
                        break;
                    }

                    string output = "";
                    using (GDSJKImpl jk = new GDSJKImpl())
                    {
                        try
                        {
                            output = GetSendText(jk.Execute(data));
                            myClientSocket.Send(Encoding.GetEncoding("GB2312").GetBytes(output));
                        }
                        catch (Exception ex)
                        {
                            output = ex.Message;
                            Console.WriteLine(output);
                            output = GetSendText(output);
                            myClientSocket.Send(Encoding.GetEncoding("GB2312").GetBytes(output));
                        }
                        
                    }
                    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    myClientSocket.Shutdown(SocketShutdown.Both);
                    myClientSocket.Close();
                    break;
                }
            }
        } 

        private static string GetSendText(string sendText)
        {
            string STX = Encoding.GetEncoding("GB2312").GetString(new byte[] { (byte)2 }, 0, 1);
            string ETX = Encoding.GetEncoding("GB2312").GetString(new byte[] { (byte)3 }, 0, 1);
            return STX + sendText + ETX;

        }

    }
}
